﻿<?php
global $m, $tab, $a;
error_log(print_r($_REQUEST, true));
$perm = &$AppUI->acl();
if (!$perm->userHasRole($AppUI->user_id, 'Accounting')) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect("m=home&tab=0");
	exit;
}
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getModuleClass('companies'));
require_once($AppUI->getMasterDataClass('mdcurrency'));
require_once($AppUI->getMasterDataClass('mdtype'));

$df = $AppUI->getPref('SHDATEFORMAT');
$cDate = new CDate();
$customer = intval( dPgetParam( $_REQUEST, 'project_company', 0 ) );

$invoiceNo = dPgetParam( $_REQUEST, 'debit_invoice', null );

$from_date = dPgetParam( $_REQUEST, 'complete_from_date', null );
if ($from_date != '') {
	$from_date = new CDate($from_date);
	$from_date = $from_date->format($df);
}
$to_date = dPgetParam( $_REQUEST, 'complete_to_date', null );
if ($to_date != '') {
	$to_date = new CDate($to_date);
	$to_date = $to_date->format($df);
}

$cus = new CCompany();
$cus->load($customer);

$obj = new CProject();
$totalRows = 0;
$projects = $obj->getList($_REQUEST, $totalRows);

$exchange = dPgetParam( $_REQUEST, CURRENCY_USD_VALUE . "_vnd", 1 );
$data = array();
$q = new DBQuery();
$i = 0;
foreach($projects as $p) {
	$p['debit_1'] = ++$i;

	$q->clear();
	$q->addTable('project_items');
	$q->addQuery('pitem_BL_AW, pitem_invoice_no, pitem_PO_no, pitem_container, pitem_container_total, pitem_delivery_date');
	$q->addWhere('pitem_project=' . $p['project_id']);
	$rawItemArr = $q->loadList();
	$pitemArr = array(
		'pitem_BL_AW' => array(),
		'pitem_invoice_no' => array(),
		'pitem_container_20f' => 0,
		'pitem_container_40f' => 0,
		'pitem_delivery_date' => array(),
	);
	foreach($rawItemArr as $row) {
		if (!in_array($row['pitem_BL_AW'], $pitemArr['pitem_BL_AW'])) {
			$pitemArr['pitem_BL_AW'][] = $row['pitem_BL_AW'];
		}
		if ($row['pitem_invoice_no'] != '' && !in_array($row['pitem_invoice_no'], $pitemArr['pitem_invoice_no'])) {
			$pitemArr['pitem_invoice_no'][] = $row['pitem_invoice_no'];
		}
		if ($row['pitem_PO_no'] != '' && !in_array($row['pitem_PO_no'], $pitemArr['pitem_invoice_no'])) {
			$pitemArr['pitem_invoice_no'][] = $row['pitem_PO_no'];
		}
		$dd = new CDate($row['pitem_delivery_date']);
		$dd = $dd->format($df);
		if (!in_array($dd, $pitemArr['pitem_delivery_date'])) {
			$pitemArr['pitem_delivery_date'][] = $dd;
		}
		switch ($row['pitem_container']) {
			case CONTAINER_20_FEET:
				$pitemArr['pitem_container_20f'] += $row['pitem_container_total'];
				break;
			case CONTAINER_40_FEET:
				$pitemArr['pitem_container_40f'] += $row['pitem_container_total'];
				break;
		}
	}

	$pitemArr['pitem_BL_AW'] = implode(', ', $pitemArr['pitem_BL_AW']);
	$pitemArr['pitem_invoice_no'] = implode(', ', $pitemArr['pitem_invoice_no']);
	$pitemArr['pitem_delivery_date'] = implode(', ', $pitemArr['pitem_delivery_date']);
	if ($pitemArr['pitem_container_20f'] < 1) unset($pitemArr['pitem_container_20f']);
	if ($pitemArr['pitem_container_40f'] < 1) unset($pitemArr['pitem_container_40f']);
	$p = arrayMerge($p, $pitemArr);

	$expenseArr = $obj->getExpenseGroup($p['project_id'], 2, NULL, $from_date, $to_date);//2 == Onbehalf expense
	$p = arrayMerge($p, $expenseArr);

//	$arArr = $obj->getAccountReceivableGroup($p['project_id']);
	$arArr = $obj->getAccountReceivableGroup($p['project_id'], $from_date, $to_date);
	if ($p['project_currency'] == CURRENCY_VND_VALUE) {
		foreach($arArr as $k => $v) {
			$arArr[$k] = $v / $exchange;
		}
	}
	$p = arrayMerge($p, $arArr);

	$data[$p['project_id']] = $p;
}
$size = count($data);
//showFVar($data);exit;
include_once(DP_BASE_DIR."/modules/reports/xtpl/xtemplate.class.php");

$xtpl = new XTemplate(DP_BASE_DIR."/modules/projects/DebitNote.xml");

// begin row
foreach($data as $row){
	$xtpl->assign('DATA',$row);
	$xtpl->parse('main.row');
	//showFVar($row[0]);
}
//exit;
// end row

// main
$xtpl->assign('CUS_NAME', $cus->company_name);
$xtpl->assign('FROM_DATE', $from_date);
$xtpl->assign('TO_DATE', $to_date);
$xtpl->assign('EXR', $size+15);
$xtpl->assign('SIZE', $size);
$xtpl->parse('main');
$out = $xtpl->out('main');

// write file
$temp_dir = dPgetConfig( 'root_dir' )."/files/temp";
if ($fp = fopen( "$temp_dir/temp$AppUI->user_id.xls", 'wb' )) {
	fwrite( $fp, $out);
	fclose( $fp );
	header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
	header("Content-Disposition: inline; filename=\"Debit_details-$invoiceNo.xls\"");
	readfile("$temp_dir/temp$AppUI->user_id.xls");
}

?>